package node.dao;

import hibernate.mappings.HibernateSessionFactory;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.type.Type;

public class CommonDao {

	/**
	 * 
	 * 
	 * @param hql
	 * @param objs
	 * @param types
	 *            :Hibernate.LONG
	 * @param start
	 * @param limit
	 * @return
	 */
	public List getListForHibernate(final String hql, final Object[] objs,
			final Type[] types, final int start, final int limit) {
		HibernateSessionFactory hsf = new HibernateSessionFactory();
		Session s = hsf.currentSession();
		Query q = s.createQuery(hql);
		if (limit != 0) {
			q.setFirstResult(start);
			q.setMaxResults(limit);
		}
		if (objs != null)
			q.setParameters(objs, types);
		List list = q.list();
		return list;
	}

	public int getTotalForHibernate(final String hql, final Object[] objs,
			final Type[] types) {
		HibernateSessionFactory hsf = new HibernateSessionFactory();
		Session s = hsf.currentSession();
		Query q = s.createQuery(hql);
		if (objs != null)
			q.setParameters(objs, types);
		return ((Integer) q.uniqueResult()).intValue();
	}

	public void saveOrUpdateEntityForH(Object o) {
		HibernateSessionFactory hsf = new HibernateSessionFactory();
		Session s = hsf.currentSession();
		s.saveOrUpdate(o);
	}
}
